home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ELECTRON / PCB_DESI / 1540.ZIP / PCBCA110.ZIP / PCBVIEW.C < prev    next >
C/C++ Source or Header  |  1992-08-27  |  14KB  |  479 lines

  1. /*
  2. ** printed circuit board displayer, Copyright (C) Randy Nevin 1989, 1990.
  3. **
  4. ** you may give this software to anyone, make as many copies as you like, and
  5. ** post it on public computer bulletin boards and file servers. you may not
  6. ** sell it or charge any fee for distribution (except for media and postage),
  7. ** remove this comment or the copyright notice from the code, or claim that
  8. ** you wrote this code or anything derived from it. you may modify the code as
  9. ** much as you want (please document clearly with comments, and maintain the
  10. ** coding style), but programs which are derived from this one are subject to
  11. ** the conditions stated here. i am providing this code so that people can
  12. ** learn from it, so if you distribute it, please include source code, not
  13. ** just executables. contact me to report bugs or suggest enhancements; i do
  14. ** not guarantee support, but i will make an effort to help you, and i want to
  15. ** act as a central clearing house for future versions. you should contact me
  16. ** before undertaking a significant development effort, to avoid reinventing
  17. ** the wheel. if you come up with an enhancement you consider particularly
  18. ** useful, i would appreciate being informed so that it can be incorporated in
  19. ** future versions. my address is: Randy Nevin, 24135 SE 16th PL, Issaquah,
  20. ** WA 98027, USA. this code is available directly from the author; just send a
  21. ** 360k floppy and a self-addressed floppy mailer with sufficient postage.
  22. **
  23. ** HISTORY
  24. ** (name        date        description)
  25. ** ----------------------------------------------------
  26. ** randy nevin        2/1/89        initial version
  27. ** randy nevin        2/11/89        released version 1.00
  28. ** randy nevin        12/27/89    fixed graphics dot-drawing bugs
  29. ** randy nevin        12/27/89    released version 1.10
  30. ** randy nevin        1/18/92        1.20, change 18x18->15x15
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35.  
  36. #ifndef VMS
  37. #include <io.h>
  38. #include <dos.h>
  39. #endif
  40.  
  41. #include <conio.h>
  42. #include <string.h>
  43. #include "cell.h"
  44.  
  45. /* WARNING: the code below assumes 640x350 16-color ega */
  46.  
  47. /* 0=black    1=blue        2=green        3=light blue        */
  48. /* 4=red    5=purple    6=brown        7=grey            */
  49. /* 8=black    9=bright blue    A=bright green    B=bright light blue    */
  50. /* C=scarlet    D=purple    E=yellow    F=white            */
  51.  
  52. /*
  53. ** the colors below work fine for me, but may not for your particular ega and
  54. ** monitor. if bright blue and light blue look the same to you, or some traces
  55. ** appear to be missing, you may want to change these constants.
  56. **
  57. ** on some egas, there appear to be gaps in the traces; i don't know why. on
  58. ** other egas, the traces look fine. this happened to me on the maximum zoom,
  59. ** but not on any other zoom level. apparently some problem with the int 10h
  60. ** interface.
  61. */
  62.  
  63. /* colors of objects */
  64. #define H    0xC    /* hole color; scarlet            */
  65. #define F    0x9    /* frontside trace color; bright blue    */
  66. #define B    0x3    /* backside trace color; light blue    */
  67. #define E    0xE    /* edge color; yellow            */
  68.  
  69. /* screen limits */
  70. #define MINHORZ    0    /* left-most pixel    */
  71. #define MAXHORZ    639    /* right-most pixel    */
  72. #define MINVERT    0    /* top-most pixel    */
  73. #define MAXVERT    349    /* bottom-most pixel    */
  74.  
  75. static int mode; /* for saving original screen mode */
  76. static int sides = 3; /* 0=holes only, 1=front only, 2=back only, 3=all */
  77.  
  78. #define MAXZOOM    3    /* maximum zoom number; minimum is 0 */
  79.  
  80. #define ZOOM0    3    /* 3x3 pixels per cell        */
  81. #define ZOOM1    6    /* 6x6 pixels per cell        */
  82. #define ZOOM2    10    /* 10x10 pixels per cell    */
  83. #define ZOOM3    15    /* 15x15 pixels per cell    */
  84.  
  85. static int zoom = 1; /* 0=3x3, 1=6x6, 2=10x10, 3=15x15 */
  86. static int zoomsize[MAXZOOM+1] = { ZOOM0, ZOOM1, ZOOM2, ZOOM3 };
  87.  
  88. /* current lower-left position */
  89. static int Xrow = 0;
  90. static int Xcol = 0;
  91.  
  92. int JustBoard = 1; /* only need the board data structure */
  93.  
  94. extern int Nrows, Ncols; /* board dimensions */
  95.  
  96. extern void InitBoard( void );
  97. extern long GetCell( int, int, int );
  98. extern void SetCell( int, int, int, long );
  99. extern int GetMode( void );
  100. extern void SetMode( int );
  101. extern void Dot( int, int, int );
  102.  
  103. void main( int, char *[] );
  104. static void doedges( void );
  105. static void doboard( void );
  106. static void map( int, int, long, long );
  107. static void plot0( int, int, char [ZOOM0][ZOOM0], int );
  108. static void plot1( int, int, char [ZOOM1][ZOOM1], int );
  109. static void plot2( int, int, char [ZOOM2][ZOOM2], int );
  110. static void plot3( int, int, char [ZOOM3][ZOOM3], int );
  111.  
  112. void main ( argc, argv ) /* input routed board, display it on the screen */
  113.     int argc;
  114.     char *argv[];
  115.     {
  116.     char *self, *p;
  117.     int r, c, rp, cp, i1, i2, i3, i4;
  118.     FILE *fp;
  119.     long x;
  120.     union REGS regs;
  121.  
  122.     printf( "Copyright (C) Randy Nevin, 1989, 1990. Version 1.20\n" );
  123.     printf( "See source code for rights granted.\n\n" );
  124.     self = argv[0];
  125.     /* get rid of initial part of path */
  126.     if ((p = strrchr( self, '\\' )) || (p = strrchr( self, ':' )))
  127.         self = ++p;
  128.     /* get rid of extension */
  129.     if ((p = strrchr( self, '.' )) && !stricmp( p, ".EXE" ))
  130.         *p = 0;
  131.     if (argc != 2) { /* need infile */
  132.         fprintf( stderr, "usage: %s infile\n", self );
  133.         exit( -1 );
  134.         }
  135.     if (!(fp = fopen( argv[1], "rb" ))) {
  136.         fprintf( stderr, "can't open %s\n", argv[1] );
  137.         exit( -1 );
  138.         }
  139.     /* fetch the board dimensions */
  140.     if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  141.         fprintf( stderr, "premature eof\n" );
  142.         exit( -1 );
  143.         }
  144.     Nrows = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  145.     if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  146.         fprintf( stderr, "premature eof\n" );
  147.         exit( -1 );
  148.         }
  149.     Ncols = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  150.     InitBoard(); /* allocate memory for data structures */
  151.     for (r = 0; r < Nrows; r++) { /* read in the board, row by column */
  152.         for (c = 0; c < Ncols; c++) {
  153.             /* first, do frontside */
  154.             if ((i1 = getc( fp )) == EOF
  155.                 || (i2 = getc( fp )) == EOF
  156.                 || (i3 = getc( fp )) == EOF
  157.                 || (i4 = getc( fp )) == EOF) {
  158.                 fprintf( stderr, "premature eof\n" );
  159.                 exit( -1 );
  160.                 }
  161.             x = (long)i1;
  162.             x |= (((long)i2) << 8);
  163.             x |= (((long)i3) << 16);
  164.             x |= (((long)i4) << 24);
  165.             SetCell( r, c, TOP, x );
  166.             /* then do backside */
  167.             if ((i1 = getc( fp )) == EOF
  168.                 || (i2 = getc( fp )) == EOF
  169.                 || (i3 = getc( fp )) == EOF
  170.                 || (i4 = getc( fp )) == EOF) {
  171.                 fprintf( stderr, "premature eof\n" );
  172.                 exit( -1 );
  173.                 }
  174.             x = (long)i1;
  175.             x |= (((long)i2) << 8);
  176.             x |= (((long)i3) << 16);
  177.             x |= (((long)i4) << 24);
  178.             SetCell( r, c, BOTTOM, x );
  179.             }
  180.         }
  181.     /* tell user what commands are available */
  182.     printf( "\t0   = holes only\n" );
  183.     printf( "\t1   = holes and top traces\n" );
  184.     printf( "\t2   = holes and bottom traces\n" );
  185.     printf( "\t3   = holes and all traces\n" );
  186.     printf( "\tz/Z = zoom one level / maximum zoom\n" );
  187.     printf( "\ts/S = shrink one level / minimum shrink\n" );
  188.     printf( "\tl/L = move left by one / move left by ten\n" );
  189.     printf( "\tr/R = move right by one / move right by ten\n" );
  190.     printf( "\tu/U = move up by one / move up by ten\n" );
  191.     printf( "\td/D = move down by one / move down by ten\n" );
  192.     printf( "\tany other key exits the program\n" );
  193.     printf( "\nPress ENTER to continue, or ^C to exit " );
  194.     regs.h.ah = 0x8; /* character input without echo */
  195.     intdos( ®s, ®s ); /* call dos to get a keystroke */
  196.     mode = GetMode(); /* save mode so can restore later */
  197.     SetMode( 0x10 ); /* 640x350 16-color mode */
  198.     doedges(); /* display board edges */
  199.     doboard(); /* display the board */
  200.     for (;;) { /* process until unrecognized keystroke */
  201.         regs.h.ah = 0x8; /* character input without echo */
  202.         intdos( ®s, ®s ); /* call dos to get a keystroke */
  203.         switch (regs.h.al) { /* determine what it is */
  204.         case '0': /* just show holes */
  205.             if (sides == 0)
  206.                 continue;
  207.             sides = 0;
  208.             break;
  209.         case '1': /* show holes and top-side traces */
  210.             if (sides == 1)
  211.                 continue;
  212.             sides = 1;
  213.             break;
  214.         case '2': /* show holes and bottom-side traces */
  215.             if (sides == 2)
  216.                 continue;
  217.             sides = 2;
  218.             break;
  219.         case '3': /* show holes and all traces */
  220.             if (sides == 3)
  221.                 continue;
  222.             sides = 3;
  223.             break;
  224.         case 'Z': /* zoom to the limit */
  225.             if (zoom == MAXZOOM)
  226.                 continue;
  227.             zoom = MAXZOOM;
  228.             break;
  229.         case 'z': /* zoom by one */
  230.             if (zoom == MAXZOOM)
  231.                 continue;
  232.             zoom++;
  233.             break;
  234.         case 'S': /* shrink to the limit */
  235.             if (zoom == 0)
  236.                 continue;
  237.             zoom = 0;
  238.             break;
  239.         case 's': /* shrink by one */
  240.             if (zoom == 0)
  241.                 continue;
  242.             zoom--;
  243.             break;
  244.         case 'L': /* left by 10 */
  245.             if (Xcol == 0)
  246.                 continue;
  247.             if (Xcol <= 10)
  248.                 Xcol = 0;
  249.             else
  250.                 Xcol -= 10;
  251.             break;
  252.         case 'l': /* left by one */
  253.             if (Xcol == 0)
  254.                 continue;
  255.             Xcol--;
  256.             break;
  257.         case 'R': /* right by 10 */
  258.             if (Xcol == Ncols-1)
  259.                 continue;
  260.             if (Xcol >= Ncols-11)
  261.                 Xcol = Ncols-1;
  262.             else
  263.                 Xcol += 10;
  264.             break;
  265.         case 'r': /* right by one */
  266.             if (Xcol == Ncols-1)
  267.                 continue;
  268.             Xcol++;
  269.             break;
  270.         case 'U': /* up by 10 */
  271.             if (Xrow == Nrows-1)
  272.                 continue;
  273.             if (Xrow >= Nrows-11)
  274.                 Xrow = Nrows-1;
  275.             else
  276.                 Xrow += 10;
  277.             break;
  278.         case 'u': /* up by one */
  279.             if (Xrow == Nrows-1)
  280.                 continue;
  281.             Xrow++;
  282.             break;
  283.         case 'D': /* down by 10 */
  284.             if (Xrow == 0)
  285.                 continue;
  286.             if (Xrow <= 10)
  287.                 Xrow = 0;
  288.             else
  289.                 Xrow -= 10;
  290.             break;
  291.         case 'd': /* down by one */
  292.             if (Xrow == 0)
  293.                 continue;
  294.             Xrow--;
  295.             break;
  296.         default:
  297.             SetMode( mode ); /* restore original screen mode */
  298.             exit( 0 );
  299.             break;
  300.             }
  301.         SetMode( 0x10 ); /* clear screen */
  302.         doedges(); /* display board edges */
  303.         doboard(); /* display the board */
  304.         }
  305.     }
  306.  
  307. static void doedges () { /* display the board edges */
  308.     int r1, c1, r2, c2, i, z;
  309.  
  310.     z = zoomsize[zoom];
  311.     /* first, calculate their virtual screen positions */
  312.     r1 = MAXVERT+(Xrow*z); /* bottom edge */
  313.     c1 = MINHORZ-(Xcol*z); /* left edge */
  314.     r2 = MAXVERT-1-((Nrows-Xrow)*z); /* top edge */
  315.     c2 = MINHORZ+1+((Ncols-Xcol)*z); /* right edge */
  316.     if (r1 >= MINVERT && r1 <= MAXVERT) /* draw bottom edge */
  317.         for (i = c1; i <= c2; i++)
  318.             if (i >= MINHORZ && i <= MAXHORZ)
  319.                 Dot( E, r1, i );
  320.     if (c1 >= MINHORZ && c1 <= MAXHORZ) /* draw left edge */
  321.         for (i = r1; i >= r2; i--)
  322.             if (i >= MINVERT && i <= MAXVERT)
  323.                 Dot( E, i, c1 );
  324.     if (r2 >= MINVERT && r2 <= MAXVERT) /* draw top edge */
  325.         for (i = c1; i <= c2; i++)
  326.             if (i >= MINHORZ && i <= MAXHORZ)
  327.                 Dot( E, r2, i );
  328.     if (c2 >= MINHORZ && c2 <= MAXHORZ) /* draw right edge */
  329.         for (i = r1; i >= r2; i--)
  330.             if (i >= MINVERT && i <= MAXVERT)
  331.                 Dot( E, i, c2 );
  332.     }
  333.  
  334. static void doboard () { /* display the board on the screen, row by column */
  335.     int r, c, rp, cp, rpd, cpd, z;
  336.     long x, y;
  337.  
  338.     z = zoomsize[zoom];
  339.     rpd = MINVERT+z; /* top-most plottable row */
  340.     cpd = MAXHORZ-z; /* right-most plottable column */
  341.     for (r = Xrow, rp = MAXVERT-1; r < Nrows && rp >= rpd; r++, rp -= z) {
  342.         for (c = Xcol, cp = MINHORZ+1; c < Ncols && cp <= cpd;
  343.                 c++, cp += z) {
  344.             x = GetCell( r, c, TOP );
  345.             y = GetCell( r, c, BOTTOM );
  346.             if (x || y) /* only map if something is there */
  347.                 map( rp, cp, x, y );
  348.             }
  349.         }
  350.     }
  351.  
  352. struct x { /* group the bit templates for an object */
  353.     long t;            /* the object type    */
  354.     char t0[ZOOM0][ZOOM0];    /* tiny zoom template    */
  355.     char t1[ZOOM1][ZOOM1];    /* small zoom template    */
  356.     char t2[ZOOM2][ZOOM2];    /* medium zoom template    */
  357.     char t3[ZOOM3][ZOOM3];    /* large zoom template    */
  358.     };
  359.  
  360. extern struct x y1[]; /* hole templates */
  361. extern struct x y2[]; /* hole-related templates */
  362. extern struct x y3[]; /* non-hole-related templates */
  363.  
  364. extern int z1; /* number of hole types */
  365. extern int z2; /* number of hole-related types */
  366. extern int z3; /* number of non-hole-related types */
  367.  
  368. #define domap1(v,c)    { for (i = 0; i < z1; i++) { \
  369.                 if (v & (y1[i].t)) { \
  370.                     if (zoom == 0) \
  371.                         plot0( rp, cp, y1[i].t0, c );\
  372.                     else if (zoom == 1) \
  373.                         plot1( rp, cp, y1[i].t1, c );\
  374.                     else if (zoom == 2) \
  375.                         plot2( rp, cp, y1[i].t2, c );\
  376.                     else if (zoom == 3) \
  377.                         plot3( rp, cp, y1[i].t3, c );\
  378.                     } \
  379.                 } } \
  380.  
  381. #define domap2(v,c)    { for (i = 0; i < z2; i++) { \
  382.                 if (v & (y2[i].t)) { \
  383.                     if (zoom == 0) \
  384.                         plot0( rp, cp, y2[i].t0, c );\
  385.                     else if (zoom == 1) \
  386.                         plot1( rp, cp, y2[i].t1, c );\
  387.                     else if (zoom == 2) \
  388.                         plot2( rp, cp, y2[i].t2, c );\
  389.                     else if (zoom == 3) \
  390.                         plot3( rp, cp, y2[i].t3, c );\
  391.                     } \
  392.                 } } \
  393.  
  394. #define domap3(v,c)    { for (i = 0; i < z3; i++) { \
  395.                 if (v & (y3[i].t)) { \
  396.                     if (zoom == 0) \
  397.                         plot0( rp, cp, y3[i].t0, c );\
  398.                     else if (zoom == 1) \
  399.                         plot1( rp, cp, y3[i].t1, c );\
  400.                     else if (zoom == 2) \
  401.                         plot2( rp, cp, y3[i].t2, c );\
  402.                     else if (zoom == 3) \
  403.                         plot3( rp, cp, y3[i].t3, c );\
  404.                     } \
  405.                 } } \
  406.  
  407. static void map ( rp, cp, v0, v1 ) /* map a cell */
  408.     int rp, cp;
  409.     long v0, v1;
  410.     {
  411.     int i;
  412.  
  413.     if (v0 & HOLE) {
  414.         domap1( v0, H ); /* plot the hole */
  415.         if (v1 && (sides & 2)) /* plot backside? */
  416.             domap2( v1, B );
  417.         if (v0 && (sides & 1)) /* plot frontside? */
  418.             domap2( v0, F );
  419.         }
  420.     else {
  421.         if (v1 && (sides & 2)) /* plot backside? */
  422.             domap3( v1, B );
  423.         if (v0 && (sides & 1)) /* plot frontside? */
  424.             domap3( v0, F );
  425.         }
  426.     }
  427.  
  428. static void plot0 ( rp, cp, obj, color ) /* plot a 3x3 template */
  429.     int rp, cp;
  430.     char obj[ZOOM0][ZOOM0];
  431.     int color;
  432.     {
  433.     int r, c;
  434.  
  435.     for (r = 0; r < ZOOM0; r++)
  436.         for (c = 0; c < ZOOM0; c++)
  437.             if (obj[r][c])
  438.                 Dot( color, rp-r, cp+c );
  439.     }
  440.  
  441. static void plot1 ( rp, cp, obj, color ) /* plot a 6x6 template */
  442.     int rp, cp;
  443.     char obj[ZOOM1][ZOOM1];
  444.     int color;
  445.     {
  446.     int r, c;
  447.  
  448.     for (r = 0; r < ZOOM1; r++)
  449.         for (c = 0; c < ZOOM1; c++)
  450.             if (obj[r][c])
  451.                 Dot( color, rp-r, cp+c );
  452.     }
  453.  
  454. static void plot2 ( rp, cp, obj, color ) /* plot a 10x10 template */
  455.     int rp, cp;
  456.     char obj[ZOOM2][ZOOM2];
  457.     int color;
  458.     {
  459.     int r, c;
  460.  
  461.     for (r = 0; r < ZOOM2; r++)
  462.         for (c = 0; c < ZOOM2; c++)
  463.             if (obj[r][c])
  464.                 Dot( color, rp-r, cp+c );
  465.     }
  466.  
  467. static void plot3 ( rp, cp, obj, color ) /* plot an 15x15 template */
  468.     int rp, cp;
  469.     char obj[ZOOM3][ZOOM3];
  470.     int color;
  471.     {
  472.     int r, c;
  473.  
  474.     for (r = 0; r < ZOOM3; r++)
  475.         for (c = 0; c < ZOOM3; c++)
  476.             if (obj[r][c])
  477.                 Dot( color, rp-r, cp+c );
  478.     }
  479.